<?php

namespace app\common\service\order\coupon_order\base;

use app\common\facade\auth\Auth;
use app\common\service\order\coupon_order\Base;
use app\common\service\order\coupon_order\CouponOrderChannelInterface;
use app\model\AgentServiceConfig;
use app\model\BaseSupplier;
use app\model\CouponOrder;
use app\model\CouponOrderSid;
use think\Exception;

/**
 * 自营下单基础类
 */
abstract class SelfEmployedBaseV2 extends Base implements CouponOrderChannelInterface
{


    /**
     * 支付的时候处理的：
     * getSupplierOrderSn
     * getPayStatus
     * getPayTime  Ymd
     * getSettledAt
     * getOutOrderId
     * getExtData
     */

    /**
     * @var CouponOrder $couponOrder
     */
    public $couponOrder;

    public $exist_fileld = 'order_id';

    abstract function getServType();

    abstract function getType();





    /**
     * 公共start
     * 本地存储订单号 自由发挥
     */
    public function getOrderId()
    {
        if (empty($this->couponOrder->order_id)) {
            return time() . rand(100000, 999999);
        }

        return $this->couponOrder->order_id;
    }


    public function getSupplierCode()
    {
        $baseSupplier = BaseSupplier::query()
            ->where('id', $this->supplier_id)
            ->find();

        if (empty($baseSupplier)) {
            throw new Exception('供应商不存在');
        }


        return $baseSupplier->code;
    }

    /**
     *
     */
    public function getMobile()
    {
        return $this->couponOrder->mobile;
    }


    /**
     * 结算价格 订单金额-代理商利润 = 代理商看到的成本价
     */
    public function getSettlePrice()
    {
        $settle_price = $this->getPayAmount() - $this->getProfit();

        if ($settle_price < 0) {
            throw new Exception('结算价小于0');
        }


        return $this->getPayAmount() - $this->getProfit();

    }

    /**
     * 公共send
     *
     * 供应商结算金额  订单金额-代理商利润-平台利润
     */
    public function getSupplierPrice()
    {
        $supplier_price = $this->couponOrder->supplier_price;
        if (!$supplier_price) {
            throw new Exception('供应商价格不能为空');
        }

        return $this->couponOrder->supplier_price;

    }


    //供应商订单号 下单之后返回的 初始值是因为有索引要求 默认生成一个
    public function getSupplierOrderSn()
    {
        return time() . rand(10000, 99999);
    }

    // 注意是 元
    public function getAmount()
    {
        //元转分
        return yuan2fen($this->couponOrder->amount);
    }

    /**
     * 总佣金 注意是 元
     * @return void
     */
    public function getOrderAllCommission()
    {
        // TODO: Implement getOrderAllCommission() method.
    }

    public function getAgentId()
    {
        return Auth::agentId();
    }

    public function getOriginAmount()
    {
        $origin_amount = $this->couponOrder->origin_amount;

        if (!$origin_amount) {
            throw new Exception('原价不能为空');
        }

        return yuan2fen($origin_amount);
    }

    public function getPayAmount()
    {
        $pay_amount = yuan2fen($this->couponOrder->pay_amount);
        if (empty($pay_amount <= 0)) {
            throw new Exception('支付金额错误');
        }
    }

    public function getPayStatus()
    {
        $payStatus = CouponOrder::C_PAY_STATUS_NO; //默认未支付

        return $payStatus;
    }

    public function getPayTime()
    {
        return null; //还没有支付
    }

    public function getUserId()
    {
        return Auth::id();
    }

    public function getStatusDesc()
    {

    }

    public function getSettledAt()
    {

    }


    public function getCnid()
    {
        return Auth::channelId();
    }

    //先做返佣
    public function getSettleType()
    {
        return SETTLE_TYPE_PROFIT;
    }

    //支付成功之后该数据替换成供应商回调
    public function getExtData()
    {
        return $this->params;
    }


    public function getOrderCreatedAt()
    {
        return date('Y-m-d H:i:s');
    }

    public function getTitle()
    {
        return $this->product_name;
    }

    public function getRefundStatus()
    {
        return CouponOrder::C_REFUND_STATUS_NO;
    }


    public function getSid()
    {

        try {
            $sid = request()->header('sid');

            if (!$sid) {
                return '';
            }
            $obj = new CouponOrderSid([
                'sid' => $sid,
            ]);

            $obj->save();

            return $obj->id;

        } catch (Exception $exception) {

            throw new Exception('sid 格式错误');

        }

    }

    public function getOutOrderId()
    {
        return '';
    }


    public function getProductName()
    {
        $product_name = $this->couponOrder->product_name;

        if (!$product_name) {
            throw new Exception('商品名称不能为空');
        }

        return $this->couponOrder->product_name;
    }

    /**
     * 结算状态
     * @return int
     */
    public function getAgentSettleStatus()
    {
        return CouponOrder::C_AGENT_SETTLE_STATUS_NO;
    }

    /**
     * 订单创建状态
     * @return
     */
    public function getStatus()
    {
        return CouponOrder::C_STATUS_NO_SUBMIT;
    }


}